package com.jiezheng.huinong.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.jiezheng.huinong.db.conf.IPeopleManageDAO;
import com.jiezheng.huinong.model.PeopleInfo;
import com.jiezheng.huinong.util.DbUtil;

public class PeopleManageDAOImpl implements IPeopleManageDAO {

	@Override
	public void addPeople(PeopleInfo people) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.peopleManage.addPeople",
					people);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public void deletePeople(String idCard) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.peopleManage.deletePeople",
					idCard);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public void modifyPeople(PeopleInfo people) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.peopleManage.modifyPeople",
					people);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}

	}

	@Override
	public PeopleInfo getPeopleInfo(String idCard) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		PeopleInfo people = null;

		try {
			people = session.selectOne(
					"com.jiezheng.huinong.peopleManage.getPeopleInfoByIdCard",
					idCard);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return people;
	}

	@Override
	public Integer getTotalPeopleNum(String cond, String deptNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("cond", cond);
		rowInfo.put("deptNo", deptNo);
		Integer totalNum = 0;
		try {
			totalNum = session.selectOne(
					"com.jiezheng.huinong.peopleManage.getTotalPeopleNum",
					rowInfo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<PeopleInfo> getPeopleInfo(String cond, String deptNo,
			Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<PeopleInfo> indexs = null;

		// 多值参数据的传递，包括起始行，与行数，实现分页
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("cond", cond);
		rowInfo.put("deptNo", deptNo);

		try {
			indexs = session.selectList(
					"com.jiezheng.huinong.peopleManage.getPeopleInfoByPage",
					rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}

	@Override
	public void changeStatus(PeopleInfo people) {

		SqlSession session = DbUtil.getSqlSessionFactory().openSession();

		try {
			session.update("com.jiezheng.huinong.peopleManage.changeStatus",
					people);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public void approveAll(PeopleInfo people) {

		SqlSession session = DbUtil.getSqlSessionFactory().openSession();

		try {
			session.update("com.jiezheng.huinong.peopleManage.approveAll",
					people);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public List<String> getAllIdCards() {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<String> idCards = null;

		try {
			idCards = session
					.selectList("com.jiezheng.huinong.peopleManage.getAllIdCards");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return idCards;
	}

	@Override
	public boolean isPeopleExist(PeopleInfo people) {

		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session.selectOne(
					"com.jiezheng.huinong.peopleManage.isPeopleExist", people);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}

		return totalNum > 0;
	}

	@Override
	public Integer getTotalPeopleNum(int status, String cond, String deptNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("cond", cond);
		rowInfo.put("status", status);
		rowInfo.put("deptNo", deptNo);
		try {
			totalNum = session
					.selectOne(
							"com.jiezheng.huinong.peopleManage.getTotalPeopleNumByStatus",
							rowInfo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<PeopleInfo> getPeopleInfo(int status, String cond,String deptNo,
			Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<PeopleInfo> indexs = null;

		// 多值参数据的传递，包括起始行，与行数，实现分页
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("cond", cond);
		rowInfo.put("deptNo", deptNo);
		rowInfo.put("status", status);

		try {
			indexs = session
					.selectList(
							"com.jiezheng.huinong.peopleManage.getPeopleInfoByPageByStatus",
							rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}

	@Override
	public Integer getTotalToAuditPeopleNum(String cond, String deptNo) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();

		// 多值参数据的传递，包括起始行，与行数，实现分页
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("cond", cond);
		rowInfo.put("deptNo", deptNo);

		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne(
							"com.jiezheng.huinong.peopleManage.getTotalToAuditPeopleNum",
							rowInfo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<PeopleInfo> getToAuditPeopleInfo(String cond, String deptNo,Integer startRow,
			Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<PeopleInfo> indexs = null;

		// 多值参数据的传递，包括起始行，与行数，实现分页
		Map<String, Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("cond", cond);
		rowInfo.put("deptNo", deptNo);

		try {
			indexs = session.selectList(
					"com.jiezheng.huinong.peopleManage.getToAuditPeopleInfo",
					rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}
}
